MAF1731 - Microstructure and Trading Systems

B.Eng Financial Engineering



I.F. Gerardo González Flores

I.F. Tania Bell Nicholls

April 2023 | Repository: Link


Lab 4

Market's Microstructure



Abstract

Throughout this laboratory, we will use the information from the Cryptocurrency's Book of Orders from various international exchanges, with the primary objective of visualizing and modeling the market's microstructure through it's characteristics and behaviors like Matchine Engine, Levels, Ask-Bid Volumes, etc.


0. Install/Load Packages and Depedencies


0.2 Python Packages

In order to run this notebook, it is necessary to have installed and/or have the requirements.txt file with the following:

  • pandas
  • numpy
  • jupyter
  • ccxt
  • ast
  • time
  • asyncio
  • os
  • plotly

0.3 Files Dependencies

The following are the file dependencies that are needed to run this notebook:

Because of the incompatability of the jupyter versions the book of orders were saved as csv.

  • binance_BTCUSDT.csv (referred to as Exchange 1)
  • binance_ETHUSDT.csv (Exchange 2)
  • bitfinex_BTCUSDT.csv (Exchange 3)
  • bitfinex_ETHUSDT.csv (Exchange 4)
  • huobi_BTCUSDT.csv (Exchange 5)
  • huobi_ETHUSDT.csv (Exchange 6)

0.4 Install Packages

In [1]:
%%capture

# Install all the pip packages in the requirements.txt
import sys
!{sys.executable} -m pip install -r requirements.txt
In [2]:
# importing all libraries
import warnings
warnings.filterwarnings('ignore')
from datetime import timedelta
import numpy as np
import pandas as pd
import ccxt
import functions as fn
import data as dt
import visualizations as vn
import plotly.express as px

1. Introduction

This laboratory focuses on using the information in the cryptocurrencies' Book of Orders to visualize and model the market's microstructure. In order to learn about a market's microstructure it is important to have all the information available, for that reason the market chosen for this project was the cryptocurrecy.

This project starts with obtaining the most accurate and current information of three different exchange markets and three different trading pairs using the ccxt library. The exchange markets chosen were Binance and Bitfinex. The trading pairs chosen were BTC/USDT, ETH/USDT and ADA/USDT. Then the information was organized and summarized taking into account the levels and total bid size and ask size.

For the visualization, graphs of the mid price were generated with the plotly library in order to compare the different time series.

To complete the objetives, the last part of the lab consisted of modeling the microstructure of the market. This was achieved through estimating the Effective Spread based on the Roll model (1984).

2. Objectives

  • Obtain, clean and analyze the information obtained from the Book of Orders
  • Visualize the mid price time series
  • Model the market's microstructure using the effective spread

3. Data Description


Exchange markets:

  • Binance
  • Bitfinex
  • Huobi

Trading pairs:

  • BTC/USDT
  • ETH/USDT

Datetime : timestamp in BO

Ask_size : volume of asks

Ask_price : price of asks

Bid_size : volume of bids

Bid_price : price of bids

Spread : difference between ask and bid

Mid_price : average Top of the Book

VWAP : volume-weighted average price

Close_price : closing price

Levels : trading levels

Data

The data obtained from the ccxt library of the Book of Orders is displayed below:

Exchange 1

In [3]:
data_files = dt.data_files
pd.read_csv("files/"+data_files[0]) # it is a dictionary but for visualization terms in the notebook it was downloaded as csv
Out[3]:
Unnamed: 0 exchange datetime orderbook
0 0 binance 2023-04-22T00:31:03.106Z {'ask_size': [2.94389, 0.0063, 2.40838, 0.0006...
1 1 binance 2023-04-22T00:31:03.623Z {'ask_size': [3.01702, 0.0063, 2.40838, 0.0006...
2 2 binance 2023-04-22T00:31:04.456Z {'ask_size': [3.01219, 0.0063, 2.40838, 0.0006...
3 3 binance 2023-04-22T00:31:05.474Z {'ask_size': [2.95339, 0.0063, 2.40838, 0.0006...
4 4 binance 2023-04-22T00:31:06.830Z {'ask_size': [3.29879, 0.0063, 2.40838, 0.0006...
... ... ... ... ...
6611 6611 binance 2023-04-22T01:30:57.604Z {'ask_size': [6.57441, 0.91683, 0.02402, 0.439...
6612 6612 binance 2023-04-22T01:30:58.268Z {'ask_size': [6.57191, 0.91683, 0.02402, 0.439...
6613 6613 binance 2023-04-22T01:30:58.921Z {'ask_size': [6.57191, 0.50942, 0.00069, 0.439...
6614 6614 binance 2023-04-22T01:30:59.436Z {'ask_size': [8.14823, 0.50942, 0.00069, 0.005...
6615 6615 binance 2023-04-22T01:30:59.936Z {'ask_size': [8.1249, 0.50942, 0.00069, 0.0058...

6616 rows × 4 columns

Exchange 2

In [4]:
pd.read_csv("files/"+data_files[1])
Out[4]:
Unnamed: 0 exchange datetime orderbook
0 0 binance 2023-04-22T00:32:26.508Z {'ask_size': [64.0842, 0.0087, 3.7989, 8.6555,...
1 1 binance 2023-04-22T00:32:27.315Z {'ask_size': [57.362, 0.0087, 3.7989, 8.6555, ...
2 2 binance 2023-04-22T00:32:28.379Z {'ask_size': [62.7687, 0.0087, 3.7989, 8.6555,...
3 3 binance 2023-04-22T00:32:29.428Z {'ask_size': [65.462, 0.0087, 3.7989, 8.6555, ...
4 4 binance 2023-04-22T00:32:30.451Z {'ask_size': [64.9541, 0.0128, 3.7989, 8.6555,...
... ... ... ... ...
5607 5607 binance 2023-04-22T01:32:19.601Z {'ask_size': [16.3299, 25.4521, 8.6827, 4.1564...
5608 5608 binance 2023-04-22T01:32:20.737Z {'ask_size': [16.3299, 25.4521, 8.6827, 4.1564...
5609 5609 binance 2023-04-22T01:32:21.827Z {'ask_size': [5.0768, 20.8054, 8.6827, 4.1564,...
5610 5610 binance 2023-04-22T01:32:22.834Z {'ask_size': [22.6148, 20.8054, 8.6827, 4.1564...
5611 5611 binance 2023-04-22T01:32:23.956Z {'ask_size': [22.7927, 20.8054, 8.6827, 4.1564...

5612 rows × 4 columns

Exchange 3

In [5]:
pd.read_csv("files/"+data_files[2])
Out[5]:
Unnamed: 0 exchange datetime orderbook
0 0 bitfinex 2023-04-24T22:31:28.293Z {'ask_size': [0.207516, 0.0878, 0.1275, 0.1291...
1 1 bitfinex 2023-04-24T22:31:29.874Z {'ask_size': [0.207516, 0.0878, 0.1275, 0.1291...
2 2 bitfinex 2023-04-24T22:31:31.858Z {'ask_size': [0.02, 0.00197795, 0.22751, 0.215...
3 3 bitfinex 2023-04-24T22:31:33.409Z {'ask_size': [0.02, 0.00197795, 0.22751, 0.215...
4 4 bitfinex 2023-04-24T22:31:34.960Z {'ask_size': [0.02, 0.00197795, 0.22751, 0.215...
... ... ... ... ...
2211 2211 bitfinex 2023-04-24T23:30:55.253Z {'ask_size': [0.0881, 0.127098, 0.127095, 0.47...
2212 2212 bitfinex 2023-04-24T23:30:56.753Z {'ask_size': [0.0881, 0.127098, 0.127095, 0.47...
2213 2213 bitfinex 2023-04-24T23:30:58.258Z {'ask_size': [0.0881, 0.127098, 0.127095, 0.47...
2214 2214 bitfinex 2023-04-24T23:31:00.379Z {'ask_size': [0.05, 0.127136, 0.05, 0.215128, ...
2215 2215 bitfinex 2023-04-24T23:31:01.914Z {'ask_size': [0.05, 0.127136, 0.05, 0.215128, ...

2216 rows × 4 columns

Exchange 4

In [6]:
pd.read_csv("files/"+data_files[3])
Out[6]:
Unnamed: 0 exchange datetime orderbook
0 0 bitfinex 2023-04-24T23:39:32.853Z {'ask_size': [3.79657, 0.0174057, 5.29616, 3.3...
1 1 bitfinex 2023-04-24T23:39:34.467Z {'ask_size': [3.79657, 0.0174057, 5.29616, 3.3...
2 2 bitfinex 2023-04-24T23:39:36.299Z {'ask_size': [3.7964, 5.29616, 4.278, 3.89207,...
3 3 bitfinex 2023-04-24T23:39:37.949Z {'ask_size': [3.7964, 5.29616, 4.278, 3.89207,...
4 4 bitfinex 2023-04-24T23:39:39.475Z {'ask_size': [3.7964, 5.29616, 4.278, 3.89207,...
... ... ... ... ...
2187 2187 bitfinex 2023-04-25T00:39:00.126Z {'ask_size': [0.13666276, 9.10187, 0.02538598,...
2188 2188 bitfinex 2023-04-25T00:39:01.711Z {'ask_size': [0.13666276, 9.10187, 0.02538598,...
2189 2189 bitfinex 2023-04-25T00:39:03.546Z {'ask_size': [0.14066693, 1.5, 9.10187, 0.0253...
2190 2190 bitfinex 2023-04-25T00:39:05.006Z {'ask_size': [0.14066693, 1.5, 9.10187, 0.0253...
2191 2191 bitfinex 2023-04-25T00:39:06.997Z {'ask_size': [0.14066693, 5.30091, 3.82607598,...

2192 rows × 4 columns

Exhange 5

In [7]:
pd.read_csv("files/"+data_files[4])
Out[7]:
Unnamed: 0 exchange datetime orderbook
0 0 huobi 2023-04-25T01:15:45.453Z {'ask_size': [0.3652, 0.036414, 0.254864, 0.14...
1 1 huobi 2023-04-25T01:15:46.289Z {'ask_size': [0.3652, 0.036414, 0.254864, 0.23...
2 2 huobi 2023-04-25T01:15:47.139Z {'ask_size': [0.3652, 0.036414, 0.254864, 0.23...
3 3 huobi 2023-04-25T01:15:47.671Z {'ask_size': [0.3652, 0.036414, 0.254864, 0.23...
4 4 huobi 2023-04-25T01:15:48.221Z {'ask_size': [0.3652, 0.036414, 0.1, 0.254864,...
... ... ... ... ...
6679 6679 huobi 2023-04-25T02:15:36.820Z {'ask_size': [0.1, 0.1, 0.256028, 0.1, 0.1, 0....
6680 6680 huobi 2023-04-25T02:15:37.360Z {'ask_size': [0.1, 0.1, 0.1282, 0.018278, 0.1,...
6681 6681 huobi 2023-04-25T02:15:37.911Z {'ask_size': [0.1, 0.256028, 0.00215, 0.1, 0.1...
6682 6682 huobi 2023-04-25T02:15:38.443Z {'ask_size': [0.1, 0.256028, 0.00215, 0.1, 0.1...
6683 6683 huobi 2023-04-25T02:15:38.992Z {'ask_size': [0.1, 0.256028, 0.00215, 0.1, 0.1...

6684 rows × 4 columns

Exchange 6

In [8]:
pd.read_csv("files/"+data_files[5])
Out[8]:
Unnamed: 0 exchange datetime orderbook
0 0 huobi 2023-04-25T01:21:29.886Z {'ask_size': [1.0083, 8.6181, 0.2742, 0.0298, ...
1 1 huobi 2023-04-25T01:21:30.790Z {'ask_size': [1.0083, 8.6181, 0.2742, 0.0298, ...
2 2 huobi 2023-04-25T01:21:31.482Z {'ask_size': [1.0083, 8.6181, 0.2742, 0.0298, ...
3 3 huobi 2023-04-25T01:21:32.299Z {'ask_size': [1.0075, 8.6181, 0.2742, 0.0298, ...
4 4 huobi 2023-04-25T01:21:32.836Z {'ask_size': [1.0083, 8.6181, 0.2742, 0.0298, ...
... ... ... ... ...
5482 5482 huobi 2023-04-25T02:21:24.573Z {'ask_size': [1.0, 0.0166, 16.0, 0.1635, 0.4, ...
5483 5483 huobi 2023-04-25T02:21:25.284Z {'ask_size': [1.0, 16.0, 0.1635, 0.4, 0.4582, ...
5484 5484 huobi 2023-04-25T02:21:25.893Z {'ask_size': [3.7183, 16.0, 0.1635, 0.4, 0.458...
5485 5485 huobi 2023-04-25T02:21:26.516Z {'ask_size': [3.7183, 16.0, 0.1635, 0.4, 0.458...
5486 5486 huobi 2023-04-25T02:21:27.166Z {'ask_size': [3.7183, 16.0, 0.1635, 0.4, 0.458...

5487 rows × 4 columns

4. Microstructure Visualizations

In order to visualize the information obtained from the Book of Orders, a DataFrame was created with the type of exchange market, timestamp, levels, volume of asks, volume of bids, total volume, mid price and VWAP. This created the right structure to then create the time series shown in the following graphs.

Exchange 1

In [10]:
series, effective_spreads = fn.consumir_orderbooks(data_files)
binancebtcusdt=series["binance_BTCUSDT"] 
binancebtcusdt.head(20)
Out[10]:
exchange timestamp levels ask_volume bid_volume total_volume mid_price vwap
0 binance 2023-04-22T00:31:03.106Z 100 27.23465 36.83498 64.06963 27284.745 27284.626979
1 binance 2023-04-22T00:31:03.623Z 100 27.74148 36.54259 64.28407 27284.745 27284.678178
2 binance 2023-04-22T00:31:04.456Z 100 27.73665 36.45203 64.18868 27284.745 27284.683408
3 binance 2023-04-22T00:31:05.474Z 100 26.95514 34.19323 61.14837 27284.745 27284.847874
4 binance 2023-04-22T00:31:06.830Z 100 27.35129 34.78255 62.13384 27284.745 27284.865674
5 binance 2023-04-22T00:31:07.794Z 100 25.38916 21.20492 46.59408 27284.745 27285.37526
6 binance 2023-04-22T00:31:08.295Z 100 30.98334 24.98436 55.9677 27284.745 27285.178585
7 binance 2023-04-22T00:31:09.166Z 100 31.25972 26.7654 58.02512 27284.745 27285.281709
8 binance 2023-04-22T00:31:10.084Z 100 28.65383 18.32443 46.97826 27284.745 27285.899916
9 binance 2023-04-22T00:31:11.001Z 100 27.98568 19.25464 47.24032 27284.745 27285.606106
10 binance 2023-04-22T00:31:11.740Z 100 28.12762 17.5985 45.72612 27284.745 27285.161785
11 binance 2023-04-22T00:31:12.557Z 100 23.58629 17.40378 40.99007 27284.745 27284.172833
12 binance 2023-04-22T00:31:13.027Z 100 23.58629 18.4617 42.04799 27284.745 27283.995855
13 binance 2023-04-22T00:31:13.490Z 100 29.034 18.50681 47.54081 27284.745 27284.881527
14 binance 2023-04-22T00:31:13.960Z 100 26.40849 18.98828 45.39677 27284.745 27284.916154
15 binance 2023-04-22T00:31:14.592Z 100 26.40718 18.96455 45.37173 27284.745 27284.918075
16 binance 2023-04-22T00:31:15.210Z 100 29.62167 26.53374 56.15541 27284.745 27284.983727
17 binance 2023-04-22T00:31:15.680Z 100 29.71058 26.83238 56.54296 27284.745 27285.355915
18 binance 2023-04-22T00:31:16.164Z 100 29.36695 26.80409 56.17104 27284.745 27285.20681
19 binance 2023-04-22T00:31:16.650Z 100 29.10464 28.78763 57.89227 27284.745 27285.455697
In [11]:
vn.plots(binancebtcusdt,"binance_BTCUSDT")

Exchange 2

In [12]:
binanceethusdt=series["binance_ETHUSDT"]
binanceethusdt.head(20)
Out[12]:
exchange timestamp levels ask_volume bid_volume total_volume mid_price vwap
0 binance 2023-04-22T00:32:26.508Z 100 526.0181 364.4239 890.442 1849.545 1849.585264
1 binance 2023-04-22T00:32:27.315Z 100 519.2959 376.2314 895.5273 1849.545 1849.578975
2 binance 2023-04-22T00:32:28.379Z 100 520.8967 376.9322 897.8289 1849.545 1849.573849
3 binance 2023-04-22T00:32:29.428Z 100 555.0478 377.0219 932.0697 1849.545 1849.576615
4 binance 2023-04-22T00:32:30.451Z 100 555.5709 381.8385 937.4094 1849.545 1849.579669
5 binance 2023-04-22T00:32:31.836Z 100 555.7909 610.7327 1166.5236 1849.545 1849.414178
6 binance 2023-04-22T00:32:32.807Z 100 561.1981 616.1253 1177.3234 1849.545 1849.415378
7 binance 2023-04-22T00:32:34.116Z 100 554.4133 602.122 1156.5353 1849.545 1849.411825
8 binance 2023-04-22T00:32:34.692Z 100 554.2554 602.3813 1156.6367 1849.545 1849.41696
9 binance 2023-04-22T00:32:35.300Z 100 554.0914 602.1508 1156.2422 1849.545 1849.417111
10 binance 2023-04-22T00:32:36.203Z 100 553.7595 602.4798 1156.2393 1849.545 1849.417117
11 binance 2023-04-22T00:32:36.799Z 100 553.954 606.0214 1159.9754 1849.545 1849.417629
12 binance 2023-04-22T00:32:37.335Z 100 559.3362 611.5001 1170.8363 1849.545 1849.418799
13 binance 2023-04-22T00:32:37.946Z 100 552.6731 605.9136 1158.5867 1849.545 1849.417471
14 binance 2023-04-22T00:32:38.522Z 100 527.5141 605.9136 1133.4277 1849.545 1849.392102
15 binance 2023-04-22T00:32:39.147Z 100 476.5074 614.609 1091.1164 1849.545 1849.364488
16 binance 2023-04-22T00:32:39.699Z 100 474.7159 623.3558 1098.0717 1849.545 1849.35878
17 binance 2023-04-22T00:32:40.303Z 100 474.7159 405.0823 879.7982 1849.545 1849.512839
18 binance 2023-04-22T00:32:40.881Z 100 475.1438 402.4077 877.5515 1849.545 1849.512884
19 binance 2023-04-22T00:32:41.439Z 100 474.6851 401.8779 876.563 1849.545 1849.512829
In [13]:
vn.plots(binanceethusdt,"binance_ETHUSDT")

Exchange 3

In [14]:
bitfinexbtcusdt=series["bitfinex_BTCUSDT"]
bitfinexbtcusdt.head(20)
Out[14]:
exchange timestamp levels ask_volume bid_volume total_volume mid_price vwap
0 bitfinex 2023-04-24T22:31:28.293Z 25 17.245608 16.735713 33.98132 27447.5 27450.16194
1 bitfinex 2023-04-24T22:31:29.874Z 25 17.245608 16.735713 33.98132 27447.5 27450.16194
2 bitfinex 2023-04-24T22:31:31.858Z 25 17.323089 17.289811 34.6129 27446.5 27448.006608
3 bitfinex 2023-04-24T22:31:33.409Z 25 17.323089 17.289811 34.6129 27446.5 27448.006608
4 bitfinex 2023-04-24T22:31:34.960Z 25 17.323089 17.289811 34.6129 27446.5 27448.006608
5 bitfinex 2023-04-24T22:31:36.719Z 25 20.666338 14.963953 35.630291 27451.0 27454.261624
6 bitfinex 2023-04-24T22:31:38.185Z 25 20.666338 14.963953 35.630291 27451.0 27454.261624
7 bitfinex 2023-04-24T22:31:39.874Z 25 20.666338 14.963953 35.630291 27451.0 27454.261624
8 bitfinex 2023-04-24T22:31:41.519Z 25 19.240226 12.44937 31.689596 27451.0 27455.880139
9 bitfinex 2023-04-24T22:31:43.042Z 25 19.240226 12.44937 31.689596 27451.0 27455.880139
10 bitfinex 2023-04-24T22:31:44.568Z 25 19.240226 12.44937 31.689596 27451.0 27455.880139
11 bitfinex 2023-04-24T22:31:46.214Z 25 18.266765 16.726171 34.992936 27450.5 27452.042073
12 bitfinex 2023-04-24T22:31:47.754Z 25 18.266765 16.726171 34.992936 27450.5 27452.042073
13 bitfinex 2023-04-24T22:31:49.207Z 25 18.266765 16.726171 34.992936 27450.5 27452.042073
14 bitfinex 2023-04-24T22:31:51.088Z 25 13.600317 18.471548 32.071865 27447.5 27445.055338
15 bitfinex 2023-04-24T22:31:52.607Z 25 13.600317 18.471548 32.071865 27447.5 27445.055338
16 bitfinex 2023-04-24T22:31:54.090Z 25 13.600317 18.471548 32.071865 27447.5 27445.055338
17 bitfinex 2023-04-24T22:31:55.740Z 25 13.620307 19.321476 32.941783 27447.5 27444.66858
18 bitfinex 2023-04-24T22:31:57.219Z 25 13.620307 19.321476 32.941783 27447.5 27444.66858
19 bitfinex 2023-04-24T22:31:58.762Z 25 13.620307 19.321476 32.941783 27447.5 27444.66858
In [15]:
vn.plots(bitfinexbtcusdt,"bitfinex_BTCUSDT")

Exchange 4

In [16]:
bitfinexethusdt=series["bitfinex_ETHUSDT"]
bitfinexethusdt.head(20)
Out[16]:
exchange timestamp levels ask_volume bid_volume total_volume mid_price vwap
0 bitfinex 2023-04-24T23:39:32.853Z 25 205.730671 106.024986 311.755657 1843.65 1844.3047
1 bitfinex 2023-04-24T23:39:34.467Z 25 205.730671 106.024986 311.755657 1843.65 1844.3047
2 bitfinex 2023-04-24T23:39:36.299Z 25 263.999265 103.804337 367.803602 1843.7 1844.730668
3 bitfinex 2023-04-24T23:39:37.949Z 25 263.999265 103.804337 367.803602 1843.7 1844.730668
4 bitfinex 2023-04-24T23:39:39.475Z 25 263.999265 103.804337 367.803602 1843.7 1844.730668
5 bitfinex 2023-04-24T23:39:41.495Z 25 282.8144 192.589952 475.404351 1844.45 1845.495149
6 bitfinex 2023-04-24T23:39:43.068Z 25 282.8144 192.589952 475.404351 1844.45 1845.495149
7 bitfinex 2023-04-24T23:39:45.004Z 25 283.086049 130.015975 413.102024 1844.55 1846.021796
8 bitfinex 2023-04-24T23:39:46.501Z 25 283.086049 130.015975 413.102024 1844.55 1846.021796
9 bitfinex 2023-04-24T23:39:48.004Z 25 283.086049 130.015975 413.102024 1844.55 1846.021796
10 bitfinex 2023-04-24T23:39:49.977Z 25 302.360097 107.204359 409.564456 1844.45 1846.392364
11 bitfinex 2023-04-24T23:39:51.574Z 25 302.360097 107.204359 409.564456 1844.45 1846.392364
12 bitfinex 2023-04-24T23:39:53.116Z 25 302.360097 107.204359 409.564456 1844.45 1846.392364
13 bitfinex 2023-04-24T23:39:54.836Z 25 333.557109 185.408505 518.965614 1844.5 1845.857993
14 bitfinex 2023-04-24T23:39:56.598Z 25 333.557109 185.408505 518.965614 1844.5 1845.857993
15 bitfinex 2023-04-24T23:39:58.168Z 25 333.557109 185.408505 518.965614 1844.5 1845.857993
16 bitfinex 2023-04-24T23:39:59.872Z 25 128.724158 111.769115 240.493273 1844.4 1844.619809
17 bitfinex 2023-04-24T23:40:01.445Z 25 128.724158 111.769115 240.493273 1844.4 1844.619809
18 bitfinex 2023-04-24T23:40:02.927Z 25 128.724158 111.769115 240.493273 1844.4 1844.619809
19 bitfinex 2023-04-24T23:40:04.625Z 25 125.845764 110.65441 236.500174 1844.4 1844.58982
In [17]:
vn.plots(bitfinexethusdt,"bitfinex_ETHUSDT")

Exchange 5

In [18]:
huobibtcusdt=series["huobi_BTCUSDT"]
huobibtcusdt.head(20)
Out[18]:
exchange timestamp levels ask_volume bid_volume total_volume mid_price vwap
0 huobi 2023-04-25T01:15:45.453Z 150 69.860066 99.58255 169.442617 27460.125 27454.633818
1 huobi 2023-04-25T01:15:46.289Z 150 70.674346 99.072718 169.747065 27460.125 27454.688527
2 huobi 2023-04-25T01:15:47.139Z 150 70.446634 99.108406 169.555041 27460.125 27454.646468
3 huobi 2023-04-25T01:15:47.671Z 150 70.446634 99.108406 169.555041 27460.125 27454.646468
4 huobi 2023-04-25T01:15:48.221Z 150 70.567026 99.103078 169.670105 27460.125 27454.661195
5 huobi 2023-04-25T01:15:48.742Z 150 70.66292 99.103078 169.765999 27460.125 27454.677954
6 huobi 2023-04-25T01:15:49.264Z 150 70.66292 99.103078 169.765999 27460.125 27454.677832
7 huobi 2023-04-25T01:15:49.790Z 150 70.0372 98.420905 168.458106 27460.015 27454.347533
8 huobi 2023-04-25T01:15:50.329Z 150 70.0372 98.420905 168.458106 27460.015 27454.347533
9 huobi 2023-04-25T01:15:50.883Z 150 69.333758 97.895802 167.229561 27460.015 27454.068518
10 huobi 2023-04-25T01:15:51.401Z 150 69.36438 98.419771 167.784152 27460.015 27454.078976
11 huobi 2023-04-25T01:15:51.937Z 150 70.267432 99.422948 169.690381 27460.015 27454.461701
12 huobi 2023-04-25T01:15:52.473Z 150 70.521745 99.422948 169.944694 27460.015 27454.469969
13 huobi 2023-04-25T01:15:52.986Z 150 70.051818 99.422948 169.474767 27460.015 27454.246133
14 huobi 2023-04-25T01:15:53.510Z 150 70.725872 99.422948 170.148821 27460.015 27454.482552
15 huobi 2023-04-25T01:15:54.026Z 150 70.78069 99.436286 170.216977 27460.015 27454.457525
16 huobi 2023-04-25T01:15:54.542Z 150 70.78069 99.436286 170.216977 27460.015 27454.457525
17 huobi 2023-04-25T01:15:55.042Z 150 70.78069 99.436286 170.216977 27460.015 27454.45847
18 huobi 2023-04-25T01:15:55.559Z 150 70.78069 97.38657 168.167261 27460.015 27454.95684
19 huobi 2023-04-25T01:15:56.076Z 150 70.111735 99.442748 169.554484 27460.015 27454.217298
In [20]:
vn.plots(huobibtcusdt,"huobi_BTCUSDT")

Exchange 6

In [21]:
huobiethusdt=series["huobi_ETHUSDT"]
huobiethusdt.head(20)
Out[21]:
exchange timestamp levels ask_volume bid_volume total_volume mid_price vwap
0 huobi 2023-04-25T01:21:29.886Z 150 541.2198 732.7801 1273.9999 1841.645 1839.493073
1 huobi 2023-04-25T01:21:30.790Z 150 510.2978 689.7019 1199.9997 1841.645 1839.592356
2 huobi 2023-04-25T01:21:31.482Z 150 535.6426 688.07 1223.7126 1841.645 1839.664595
3 huobi 2023-04-25T01:21:32.299Z 150 534.3126 738.1453 1272.4579 1841.645 1839.36772
4 huobi 2023-04-25T01:21:32.836Z 150 538.993 738.1453 1277.1383 1841.645 1839.419296
5 huobi 2023-04-25T01:21:33.364Z 150 539.0244 739.2395 1278.2639 1841.645 1839.420376
6 huobi 2023-04-25T01:21:34.033Z 150 536.805 739.1446 1275.9496 1841.645 1839.431702
7 huobi 2023-04-25T01:21:34.620Z 150 539.1775 738.3519 1277.5294 1841.645 1839.413361
8 huobi 2023-04-25T01:21:35.260Z 150 542.1254 738.3519 1280.4773 1841.645 1839.464228
9 huobi 2023-04-25T01:21:36.006Z 150 542.1254 738.3519 1280.4773 1841.645 1839.464228
10 huobi 2023-04-25T01:21:36.761Z 150 539.1859 738.3509 1277.5368 1841.645 1839.415614
11 huobi 2023-04-25T01:21:37.320Z 150 539.1834 738.3534 1277.5368 1841.645 1839.415533
12 huobi 2023-04-25T01:21:37.866Z 150 539.6179 739.3534 1278.9713 1841.645 1839.419685
13 huobi 2023-04-25T01:21:38.489Z 150 539.6339 739.3534 1278.9873 1841.645 1839.419872
14 huobi 2023-04-25T01:21:39.052Z 150 539.6179 739.3534 1278.9713 1841.645 1839.419685
15 huobi 2023-04-25T01:21:39.733Z 150 558.3571 743.7346 1302.0917 1841.645 1839.345298
16 huobi 2023-04-25T01:21:40.305Z 150 561.1489 743.7346 1304.8835 1841.645 1839.395125
17 huobi 2023-04-25T01:21:40.982Z 150 557.8189 743.8985 1301.7174 1841.645 1839.348112
18 huobi 2023-04-25T01:21:41.600Z 150 557.8189 743.8985 1301.7174 1841.645 1839.348112
19 huobi 2023-04-25T01:21:42.119Z 150 559.8636 729.9327 1289.7963 1841.645 1839.315805
In [22]:
vn.plots(huobiethusdt,"huobi_ETHUSDT")


5. Microstructure Modeling

In order to model the microstructure of the market, the Effective Spread was calculated based on Roll's model (1984). This model provides a simple market microstructure model that allows one to estimate the bid-ask spread from observed transaction prices alone, without information on the underlying bid-ask price quotes and the order. The model developed a simple model to estimate the bid–ask spread using the serial covariance of observable trade prices. The model used in this project is the following:

$$2\sqrt{|COV(\Delta P_t,\Delta P_{t-1}|}$$

using the series in $t-5$ for $\Delta P_{t-1}$.

Exchange 1

In [23]:
effective_spreads["binance_BTCUSDT"].head()
Out[23]:
timestamp close spread effective spread
0 2023-04-22T00:31:03.106Z 27284.75 12.1999 0.016733
1 2023-04-22T00:31:03.623Z 27284.74 11.878 0.016733
2 2023-04-22T00:31:04.456Z 27284.74 11.822 0.020000
3 2023-04-22T00:31:05.474Z 27284.75 12.1273 0.016733
4 2023-04-22T00:31:06.830Z 27284.75 12.066 0.016733

Exchange 2

In [24]:
effective_spreads["binance_ETHUSDT"].head()
Out[24]:
timestamp close spread effective spread
0 2023-04-22T00:32:26.508Z 1849.54 1.4236 0.014142
1 2023-04-22T00:32:27.315Z 1849.55 1.4475 0.008944
2 2023-04-22T00:32:28.379Z 1849.54 1.4475 0.000000
3 2023-04-22T00:32:29.428Z 1849.54 1.4432 0.008944
4 2023-04-22T00:32:30.451Z 1849.54 1.4266 0.008944

Exchange 3

In [25]:
effective_spreads["bitfinex_BTCUSDT"].head()
Out[25]:
timestamp close spread effective spread
0 2023-04-24T22:31:28.293Z 3870.1 34.64 0.0
1 2023-04-24T22:31:29.874Z 3870.1 34.64 0.0
2 2023-04-24T22:31:31.858Z 3870.1 32.52 0.0
3 2023-04-24T22:31:33.409Z 3870.1 32.52 0.0
4 2023-04-24T22:31:34.960Z 3870.1 32.52 0.0

Exchange 4

In [26]:
effective_spreads["bitfinex_ETHUSDT"].head(15)
Out[26]:
timestamp close spread effective spread
0 2023-04-24T23:39:32.853Z 133.5 2.784 0.0
1 2023-04-24T23:39:34.467Z 133.5 2.784 0.0
2 2023-04-24T23:39:36.299Z 133.5 2.92 0.0
3 2023-04-24T23:39:37.949Z 133.5 2.92 0.0
4 2023-04-24T23:39:39.475Z 133.5 2.92 0.0
5 2023-04-24T23:39:41.495Z 133.5 3.32 0.0
6 2023-04-24T23:39:43.068Z 133.5 3.32 0.0
7 2023-04-24T23:39:45.004Z 133.5 3.328 0.0
8 2023-04-24T23:39:46.501Z 133.5 3.328 0.0
9 2023-04-24T23:39:48.004Z 133.5 3.328 0.0
10 2023-04-24T23:39:49.977Z 133.5 3.464 0.0
11 2023-04-24T23:39:51.574Z 133.5 3.464 0.0
12 2023-04-24T23:39:53.116Z 133.5 3.464 0.0
13 2023-04-24T23:39:54.836Z 133.5 3.284 0.0
14 2023-04-24T23:39:56.598Z 133.5 3.284 0.0

Exchange 5

In [27]:
effective_spreads["huobi_BTCUSDT"].head(25)
Out[27]:
timestamp close spread effective spread
0 2023-04-25T01:15:45.453Z 27465.0 137.599667 0.000000
1 2023-04-25T01:15:46.289Z 27465.0 136.811733 0.000000
2 2023-04-25T01:15:47.139Z 27465.0 136.415267 0.000000
3 2023-04-25T01:15:47.671Z 27465.0 136.415267 0.000000
4 2023-04-25T01:15:48.221Z 27465.0 135.573267 0.000000
5 2023-04-25T01:15:48.742Z 27465.0 135.5664 0.000000
6 2023-04-25T01:15:49.264Z 27465.0 135.5662 0.000000
7 2023-04-25T01:15:49.790Z 27465.0 136.971867 0.000000
8 2023-04-25T01:15:50.329Z 27465.0 136.971867 0.000000
9 2023-04-25T01:15:50.883Z 27465.0 139.565667 0.000000
10 2023-04-25T01:15:51.401Z 27465.0 138.3316 0.000000
11 2023-04-25T01:15:51.937Z 27465.0 135.999333 0.000000
12 2023-04-25T01:15:52.473Z 27465.0 135.145 0.000000
13 2023-04-25T01:15:52.986Z 27465.0 134.8084 0.000000
14 2023-04-25T01:15:53.510Z 27465.0 134.313133 0.000000
15 2023-04-25T01:15:54.026Z 27465.0 131.7038 0.000000
16 2023-04-25T01:15:54.542Z 27465.0 131.7038 0.000000
17 2023-04-25T01:15:55.042Z 27465.0 131.6848 0.000000
18 2023-04-25T01:15:55.559Z 27465.0 132.6906 0.000000
19 2023-04-25T01:15:56.076Z 27465.0 132.175 4.472136
20 2023-04-25T01:15:56.594Z 27465.0 132.175 4.476642
21 2023-04-25T01:15:57.126Z 27465.0 131.684067 4.472225
22 2023-04-25T01:15:57.637Z 27465.0 131.684067 4.472225
23 2023-04-25T01:15:58.166Z 27465.0 131.684 4.472225
24 2023-04-25T01:15:58.694Z 27460.0 131.684 0.028284

Exchange 6

In [28]:
effective_spreads["huobi_ETHUSDT"].head(15)
Out[28]:
timestamp close spread effective spread
0 2023-04-25T01:21:29.886Z 1841.4 22.840267 0.000000
1 2023-04-25T01:21:30.790Z 1841.4 23.134133 0.000000
2 2023-04-25T01:21:31.482Z 1841.4 22.9842 0.000000
3 2023-04-25T01:21:32.299Z 1841.4 23.106067 0.000000
4 2023-04-25T01:21:32.836Z 1841.4 23.035267 0.000000
5 2023-04-25T01:21:33.364Z 1841.4 22.817933 0.000000
6 2023-04-25T01:21:34.033Z 1841.4 23.1178 0.000000
7 2023-04-25T01:21:34.620Z 1841.4 23.0344 0.000000
8 2023-04-25T01:21:35.260Z 1841.4 23.0222 0.000000
9 2023-04-25T01:21:36.006Z 1841.4 23.0222 0.223607
10 2023-04-25T01:21:36.761Z 1841.4 23.033 0.223607
11 2023-04-25T01:21:37.320Z 1841.4 23.032933 0.223607
12 2023-04-25T01:21:37.866Z 1841.4 22.901133 0.223607
13 2023-04-25T01:21:38.489Z 1841.4 22.901267 0.223607
14 2023-04-25T01:21:39.052Z 1841.65 22.901133 0.000000

6. Conclusions


Tania Bell Nicholls:

Understanding the microstructure of exchange markets is essencial for trading as it provides the information to know how the prices in the market are determined. Knowing how the ask, bid, spread and other elements of the pricing can allow us to improve out decision making. The informed decision making can lead to more efficient strategies as it allows us to identify the possible opportunities that arise. This can also be achieved by analyzing the market trends which can also be observed through the microstrucure.

In this lab we were able to understand the relationship between the asks and the bids and how most markets were. Once we knew how the market prioritizes the buys and sells we can understand the changes in prices and spreads which then leads us to knowing how the market works and therefore being able to predict it.

A situation that complicated the execution of this lab was the low volume and movement within the exchange market which generated non conclusive results in terms of the spread. Although we found ourselved with this problem, we were able to analyze some of the exchange markets and some of the trading pairs.

With the different graphs for the mid price, it was interesting to see how the behavior of the mid price varied a lot between each exchange and each trading pair as well as the differences between the effective spread. In these cases we can observe a more efficient market when the effective spread if lower. For the exchange market Huobi we can see a less efficient market in the BTC/USDT trading pair so it could be an indicator that it would be a good decision to avoid that market for that pair. These types of decisions are the ones that can be made when the microstructure of the exchange market is analyzed.

Gerardo González

El análisis de microestructuras es una herramienta valiosa para obtener información detallada sobre el comportamiento del mercado. A través de este análisis, puedo identificar patrones de comportamiento del mercado, como la presencia de liquidez, volatilidad y efectos de retroceso, lo que me permite tomar decisiones informadas sobre mis estrategias de inversión.

Además, el análisis de microestructuras ayuda a evaluar la eficiencia del mercado y a identificar oportunidades de negociación al detectar discrepancias de precios entre diferentes mercados. También me permite monitorear la actividad de los inversores y el flujo de órdenes en el mercado, lo que me ayuda a tomar decisiones de inversión más fundamentadas.

Otro beneficio del análisis de microestructuras es que me permite evaluar la calidad de ejecución de mis órdenes, incluyendo la velocidad y la certeza de la ejecución. Esto me da confianza en mis decisiones de inversión y me ayuda a mantenerme informado sobre el rendimiento de mis inversiones.En el laboratorio nos pudimos dar cuenta de las tendencias de cuales exchange markets eran mejores, observamos que el mercado de Huobi era el menos eficiente en el BTC/USDT a comparación del Binance y Bitfinex, por eso con este tipo de analisis pordemos escoger que es lo que nos conviene más a nosotros.

En resumen, el análisis de microestructuras me proporciona información valiosa sobre el comportamiento del mercado y la eficiencia del mismo, lo que me permite tomar decisiones informadas y estratégicas sobre mis inversiones.

In [ ]:
 


7. References


Nimalendran, M. (2010). Roll Model. Encyclopedia of Quantitative Finance. https://doi.org/10.1002/9780470061602.eqf18014

Semiparametric Identification of the Bid-Ask Spread in Extended. (2015). Core. https://core.ac.uk/download/pdf/146463242.pdf